package top.sehnsucht;

import org.apache.log4j.Logger;
import top.sehnsucht.dao.ExcelDao;
import top.sehnsucht.dao.impl.ExcelDaoImpl;
import top.sehnsucht.service.ExcelService;
import top.sehnsucht.service.impl.ExcelServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description:
 * @Author: Cai
 * @CreateTime: 2021/11/20
 */

public class DownloadServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Logger logger = Logger.getLogger(this.getClass());
        String path = req.getSession().getServletContext().getRealPath("/") + "\\excel";
        String name = req.getParameter("name");
        logger.info(path);
        logger.info(name);
        ExcelDao dao = new ExcelDaoImpl();
        ExcelService service = new ExcelServiceImpl(dao);
        service.DataToExcel(name,path);

//        resp.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
//        resp.setHeader("Location", path + "\\" + name);


        resp.setContentType("application/vnd.ms-excel");
        req.getRequestDispatcher("/excel/" + name + ".xls").forward(req,resp);

        req.getRequestDispatcher("/list").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
